/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package calendarioservice;

import calendarioentities.Cita;
import calendarioentities.Calendario;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 *
 * @author alyer
 */
@Stateless
@Path("cita")
public class CitaFacadeREST extends AbstractFacade<Cita> {
    @PersistenceContext(unitName = "calendarioRESTFULPU")
    private EntityManager em;
    
    @Context
    private UriInfo context;

    public CitaFacadeREST() {
        super(Cita.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Cita entity) {
        super.create(entity);
        
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public Response edit(Cita entity) {
        super.edit(entity);
         URI createduri = null;
        try {
          createduri = new URI(context.getAbsolutePath().toString()+"/"+entity.getIdcita());
        } catch (URISyntaxException ex) { }
        return Response.status(Response.Status.NO_CONTENT).header("Location", createduri).build();
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Cita find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Cita> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Cita> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
     /* Creacion de nuevos metodos 
     * Alexander Romero
     * Daniel Vasquez
     */
      /**
     * @descripcion: devuelve la cuenta de citas dentro de un periodo de tiempo
     * @param fromDate
     * @param toDate
     * @return 
     */
    @GET
    @Path("count/{from}/{to}")
    @Produces("text/plain")
    public String countRange(@PathParam("from") @DefaultValue("0") String fromDate, 
            @PathParam("to") @DefaultValue("0") String toDate) {
        
        List<Cita> listaCitas = this.findAll();
        List<Cita> listaFinalCitas = new ArrayList<>();
        
        if(fromDate.equals("0") || toDate.equals("0")){
         return String.valueOf(listaCitas.size());}
        
        try {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            for (Cita c : listaCitas) {
                
                Date from = df.parse(fromDate);
                Date to = df.parse(toDate);
                
                if (c.getFechainicio().after(from) && c.getFechafin().before(to))
                    listaFinalCitas.add(c);
            }
        }
        catch (ParseException ex){
            ex.printStackTrace();
        }
        
        return String.valueOf(listaFinalCitas.size());
    }
}
